package com.leyou.order.web;

import com.leyou.order.dto.OrderDTO;
import com.leyou.order.service.OrderService;
import com.leyou.order.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @package com.leyou.order.web
 * @description:
 * @author: 许超越
 * @date 2019/7/12 11:57
 * @version: V1.0
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 提交创建订单
     * @param orderDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Long> createOrder(@RequestBody OrderDTO orderDTO) {
        return ResponseEntity.status(HttpStatus.CREATED).body(orderService.createOrder(orderDTO));
    }


    /**
     * 通过订单id查询订单
     * @param orderId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<OrderVO> queryOrderById(@PathVariable("id") Long orderId){
        return ResponseEntity.ok(orderService.queryOrderById(orderId));
    }


    /**
     * 统一下单，生成支付二维码
     * @param id
     * @return
     */
    @GetMapping("url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable Long id) {
        return ResponseEntity.ok(orderService.createPayUrl(id));
    }


    /**
     * 获取订单支付的状态
     * @param orderId
     * @return
     */
    @GetMapping("state/{id}")
    public ResponseEntity<Integer> queryPayState(@PathVariable("id") Long orderId) {
        return ResponseEntity.ok(orderService.queryPayState(orderId));
    }
}
